<?php

Yii::import('application.models._base.BaseItem');

class Item extends BaseItem
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function rules() {
        return array(
            array('uid, image_path, create_time', 'required','on'=>'create'),
            array('uid, is_show, is_delete, create_time', 'numerical', 'integerOnly'=>true),
            array('image_path', 'length', 'max'=>100),
            array('share_type', 'length', 'max'=>6),
            array('tags, content', 'length', 'max'=>255),
            array('share_type, is_show, is_delete', 'default', 'setOnEmpty' => true, 'value' => null),
            array('tags,content','safe'),
            array('id, uid, image_path, share_type, is_show, is_delete, create_time, tags, content', 'safe', 'on'=>'search'),
        );
    }

    public function getItemPk($id){
        return $this->findByPk($id);
    }

    public function editItem($id,$content){
        $info = $this->findByPk($id);
        $info->content = $content;
        $info->save();
    }
    public function createItem($uid,$image,$tags,$content){
        $item_db = new Item();
        $item_db->setScenario('create');
        $item_data = array(
                'uid'=>$uid,
                'image_path'=>$image,
                'share_type'=>'upload',
                'is_show'=>1,
                'is_delete'=>0,
                'create_time'=>time(),
                'tags'=>'',
                'content'=>CHtml::encode(Yii::app()->WordFilter->replacement($content))
            );
        $item_db->setAttributes($item_data);
        $data = array();
        if ($item_db->save()) {
            $id = $item_db->attributes['id'];
            //标签标查询
            $tag_arr = array();
            $new_tags = '';
            if($tags){
                $tags = trim($tags); //过滤首尾字符
                $tag_arr = explode(' ',$tags);
                foreach($tag_arr as $_k=>$_v){
                    $tid = Tag::model()->findTag($_v);
                    $new_tags .= $_v.'|'.$tid.' ';
                }
                $new_tags = trim($new_tags);
                Item::model()->updateByPk($id,array('tags'=>$new_tags));
            }
            $data['status'] = 1;
            $data['id'] = $id;
        }else{
            $data['status'] = 0;
            $data['message'] = $this->showError($item_db->getErrors());
        }
        return $data;
    }
    //验证这个收藏是否是这个用户的
    public function checkItem($id,$uid){
        $data = $this->findByPk($id);
        if($data->uid == $uid){
            $return = 1;
        }else{
            $return = 0;
        }
        return $return;
    }
    //显示错误
    public function showError($errors){
        $array = array();
        if($errors){
            foreach($errors as $key=>$value){
                foreach($value as $v){
                    $array[] = $v;
                }
            }
            return $array[0];
        }else{
            return true;
        }
    }
}